package cn.xianshu.basic.service.impl;

import cn.xianshu.basic.mapper.BasicMapper;
import cn.xianshu.basic.query.BasicQuery;
import cn.xianshu.basic.service.IBasicService;
import cn.xianshu.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {

    @Autowired
    private BasicMapper<T> basicMapper;


    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @Override
    public T findById(Long id) {
        return basicMapper.loadById(id);
    }


    /**
     * 查询所有
     * @return
     */
    @Override
    public List<T> findAll() {
        return basicMapper.loadAll();
    }

    /**
     * 添加
     * @param t
     */
    @Override
    public void insert(T t) {
        basicMapper.insert(t);
    }


    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        basicMapper.remove(id);
    }


    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public PageList<T> queryList(BasicQuery query) {
        // 总条数
        Integer totals = basicMapper.count(query);
        // 当前页数据
        List<T> rows = basicMapper.queryData(query);
        return new PageList<>(totals, rows);
    }

    /*@Override
    @Transactional
    public void add(T t) {
        basicMapper.insert(t);
    }*/


    /**
     * 修改
     * @param t
     */
    @Override
    @Transactional
    public void update(T t) {
        basicMapper.update(t);
    }


    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void Bydelete(List<Long> ids) {
        basicMapper.Bydelete(ids);
    }

}
